<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
  <meta content="" name="keywords">

  <title> Blog - </title>
  <meta name="description" content="" />
  <meta property="og:title" content="Blog" />
  <meta name="twitter:title" content="Blog" />
  <meta name="description" content="一个记录学习生活的场所. 大概会有五个方面内容: Thoughts: My thoughts and attitudes about learning everything in life. 思考：我对学习、生活中一切事物的想法和态度。 Critique: My comment, or about technology, or about life, or about something or a movie. 评论：我的评论，或关于技术，或关于生活、或关于某些事情或者一场电影。 Travel: My travel record, of course, may be a business trip haha. 旅行：我的旅行记录，当然可能是出差记录哈哈。 Moment: Some moments that I feel something and eager to write it down. Fragments in my life. Short memos. 片刻：有些时刻我觉得有些东西并且急于把它写下来. 我生命中的碎片 简短的备忘录。 Gallery: Pictures I&rsquo;d like to">
  <meta property="og:description" content="一个记录学习生活的场所. 大概会有五个方面内容: Thoughts: My thoughts and attitudes about learning everything in life. 思考：我对学习、生活中一切事物的想法和态度。 Critique: My comment, or about technology, or about life, or about something or a movie. 评论：我的评论，或关于技术，或关于生活、或关于某些事情或者一场电影。 Travel: My travel record, of course, may be a business trip haha. 旅行：我的旅行记录，当然可能是出差记录哈哈。 Moment: Some moments that I feel something and eager to write it down. Fragments in my life. Short memos. 片刻：有些时刻我觉得有些东西并且急于把它写下来. 我生命中的碎片 简短的备忘录。 Gallery: Pictures I&rsquo;d like to">
  <meta name="twitter:description" content="一个记录学习生活的场所. 大概会有五个方面内容: Thoughts: My thoughts and attitudes about learning everything in life. 思考：我对学习、生活中一切事物的想法和态度。 Critique: My comment, or about technology, or about life, or about something or a …">
  <meta name="author" content="{Description { .Site.Author.name }}"/>
  <meta name="twitter:card" content="summary" />
  <meta property="og:url" content="https://systemime.github.io/blog/" />
  <meta property="og:type" content="website" />
  <meta property="og:site_name" content="听风的小站" />

  <meta name="generator" content="Hugo 0.67.0" />
  <link rel="canonical" href="https://systemime.github.io/blog/" />
  <link rel="alternate" href="https://systemime.github.io/index.xml" type="application/rss+xml" title="听风的小站">
  <script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/jquery-1.12.4.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" integrity="sha384-wITovz90syo1dJWVh32uuETPVEtGigN07tkttEqPv+uR2SE/mbQcG7ATL28aI9H0" crossorigin="anonymous">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  
  
  <link rel="stylesheet" href="https://systemime.github.io/css/main.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/my.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/pace.css" />
  <link rel="stylesheet" href="https://cdn.plyr.io/3.4.7/plyr.css">
  <link rel="stylesheet" href="https://systemime.github.io/css/lightgallery.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/baguetteBox.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/search.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/reward.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/share.min.css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" />
  <link rel="stylesheet" href="https://systemime.github.io/css/pygment_highlights.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/highlight.min.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/toc.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/lightbox.css" />
  <link rel="stylesheet" href="https://systemime.github.io/live2d/css/live2d.css" />
  
  <link href="https://systemime.github.io/css/video-js.css" rel="stylesheet">


<meta name="baidu-site-verification" content="g8IYR9SNLF" />
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cffe439e37449bb1c07ab26ab56484bb";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

<link rel="stylesheet" href="https://systemime.github.io/css/prism.css" />









<script src="https://systemime.github.io/js/pace.min.js" data-no-instant></script>



<script src="https://systemime.github.io/js/instantclick.min.js" data-no-instant></script>
   <script data-no-instant>
   InstantClick.on('change', function(isInitialLoad) {
     if (isInitialLoad === false) {
       if (typeof MathJax !== 'undefined') 
         MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
       if (typeof prettyPrint !== 'undefined') 
         prettyPrint();
       if (typeof _hmt !== 'undefined')  
         _hmt.push(['_trackPageview', location.pathname + location.search]);
       if (typeof ga !== 'undefined')  
           ga('send', 'pageview', location.pathname + location.search);
     }
   });
   InstantClick.init();
</script>




<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gist-embed/2.7.1/gist-embed.min.js"></script>


<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
  (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: "ca-pub-3925981084585036",
    enable_page_level_ads: true
  });
</script>



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/photoswipe.min.css" integrity="sha256-sCl5PUOGMLfFYctzDW3MtRib0ctyUvI9Qsmq2wXOeBY=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/default-skin/default-skin.min.css" integrity="sha256-BFeI1V+Vh1Rk37wswuOYn5lsTcaU96hGaI7OUVCLjPc=" crossorigin="anonymous" />



<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

<div class="pswp__bg"></div>

<div class="pswp__scroll-wrap">
    
    <div class="pswp__container">
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
    </div>
    
    <div class="pswp__ui pswp__ui--hidden">
    <div class="pswp__top-bar">
      
      <div class="pswp__counter"></div>
      <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
      <button class="pswp__button pswp__button--share" title="Share"></button>
      <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
      <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
      
      
      <div class="pswp__preloader">
        <div class="pswp__preloader__icn">
          <div class="pswp__preloader__cut">
            <div class="pswp__preloader__donut"></div>
          </div>
        </div>
      </div>
    </div>
    <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
      <div class="pswp__share-tooltip"></div>
    </div>
    <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
    </button>
    <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
    </button>
    <div class="pswp__caption">
      <div class="pswp__caption__center"></div>
    </div>
    </div>
    </div>
</div>

</head>

  <body>
    <nav class="navbar navbar-default navbar-fixed-top navbar-custom">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="https://systemime.github.io">听风的小站</a>
    </div>
    <div class="collapse navbar-collapse" id="main-navbar">
      <ul class="nav navbar-nav navbar-right">
        
          
            <li>
		    <a title="博客" href="https://systemime.github.io/blog/">博客</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="片刻" href="https://systemime.github.io/moment/">片刻</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="关于" href="https://systemime.github.io/about/">关于</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="留言" href="https://systemime.github.io/message/">留言</a>
              
              
            </li>
          
        

        

        
        
      </ul>
    </div>

    <div class="avatar-container">
      <div class="avatar-img-border">
        
      </div>
    </div>

  </div>
</nav>






    
  <header class="header-section ">
    <div class="intro-header no-img">
      <div class="container">
        <div class="row">
          <div class="col-lg-12 col-md-12 col-md-offset-0">
            <div class="page-heading">
              <h1>Blog</h1>
              <hr class="small">
            </div>
          </div>
        </div>
      </div>
    </div>
  </header>

    
  <div class="container" role="main">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        
          <div class="well">
            <p>一个记录学习生活的场所. 大概会有五个方面内容:</p>
<hr>
<ul>
<li>Thoughts: My thoughts and attitudes about learning everything in life.<br>
<!-- raw HTML omitted -->思考：我对学习、生活中一切事物的想法和态度。<!-- raw HTML omitted --></li>
<li>Critique: My comment, or about technology, or about life, or about something or a movie.<br>
<!-- raw HTML omitted -->评论：我的评论，或关于技术，或关于生活、或关于某些事情或者一场电影。<!-- raw HTML omitted --></li>
<li>Travel: My travel record, of course, may be a business trip haha.<br>
<!-- raw HTML omitted -->旅行：我的旅行记录，当然可能是出差记录哈哈。<!-- raw HTML omitted --></li>
<li>Moment: Some moments that I feel something and eager to write it down. Fragments in my life. Short memos.
<!-- raw HTML omitted --><!-- raw HTML omitted -->片刻：有些时刻我觉得有些东西并且急于把它写下来. 我生命中的碎片 简短的备忘录。<!-- raw HTML omitted --></li>
<li>Gallery: Pictures I&rsquo;d like to share. My favourite ones.
<!-- raw HTML omitted --><!-- raw HTML omitted -->画廊：我想分享的图片. 我最喜欢的。<!-- raw HTML omitted --></li>
</ul>
<p>博客无序，随需随记</p>

          </div>
        
        <div class="posts-list">
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-04/%E5%9F%BA%E4%BA%8Eseleniumbeautifulsoup%E4%B8%8Egoose3%E7%9A%84%E5%8A%A8%E6%80%81%E7%BD%91%E7%BB%9C%E7%88%AC%E8%99%AB%E4%BB%A5%E4%B8%9C%E6%96%B9%E8%A7%86%E9%87%8E%E4%B8%BA%E4%BE%8B/">
                <h2 class="post-title">基于selenium、BeautifulSoup与goose3的动态网络爬虫（以东方视野为例）</h2>

                
              </a>

              <p class="post-meta">
                Posted on April 27, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="爬虫所需环境">爬虫所需环境</h2>
<ul>
<li>python3.6.5</li>
<li>selenium</li>
<li>selenium对应浏览器驱动</li>
<li>BeautifulSoup</li>
<li>goose3</li>
</ul>
<h2 id="保证网速运行下面代码">保证网速运行下面代码</h2>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="c1"># encoding=utf8</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.support</span> <span class="kn">import</span> <span class="n">expected_conditions</span> <span class="k">as</span> <span class="n">EC</span>
<span class="kn">from</span> <span class="nn">bs4</span> <span class="kn">import</span> <span class="n">BeautifulSoup</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">csv</span>
<span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>
<span class="kn">from</span> <span class="nn">goose3</span> <span class="kn">import</span> <span class="n">Goose</span>
<span class="kn">from</span> <span class="nn">goose3.text</span> <span class="kn">import</span> <span class="n">StopWordsChinese</span>  <span class="c1"># 中文库</span>
<span class="kn">import</span> <span class="nn">shutil</span><span class="o">,</span> <span class="nn">os</span>

<span class="k">def</span> <span class="nf">nChrome</span><span class="p">():</span>  <span class="c1"># 后台浏览器模式</span>
    <span class="n">chrome_options</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">ChromeOptions</span><span class="p">()</span>
    <span class="n">chrome_options</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--headless&#39;</span><span class="p">)</span>
    <span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Chrome</span><span class="p">(</span><span class="n">chrome_options</span><span class="o">=</span><span class="n">chrome_options</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">driver</span>

<span class="c1"># 前台浏览器模式</span>
<span class="k">def</span> <span class="nf">openChrome</span><span class="p">():</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">option</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">ChromeOptions</span><span class="p">()</span>
        <span class="n">option</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;disable-infobars&#39;</span><span class="p">)</span>
        <span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Chrome</span><span class="p">(</span><span class="n">chrome_options</span><span class="o">=</span><span class="n">option</span><span class="p">)</span>
        <span class="c1"># driver.maximize_window()  # 最大化浏览器</span>
        <span class="k">return</span> <span class="n">driver</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="s2">&#34;有错误发生，驱动程序缺失或浏览器版本不匹配，或环境变量未配置&#34;</span><span class="p">)</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
        <span class="nb">exit</span><span class="p">()</span>

<span class="c1"># 爬虫主体</span>
<span class="k">def</span> <span class="nf">Open_w</span><span class="p">(</span><span class="n">w_num</span><span class="p">):</span>
    <span class="n">base_url</span> <span class="o">=</span> <span class="s2">&#34;http://finance.eastmoney.com/a/cdfsd_{page}.html&#34;</span>
    <span class="n">num</span> <span class="o">=</span> <span class="mi">1</span>  <span class="c1"># 文章计数</span>
    <span class="n">page</span> <span class="o">=</span> <span class="mi">1</span>  <span class="c1"># 页数计数</span>
    <span class="n">entry</span> <span class="o">=</span> <span class="mi">1</span>  <span class="c1"># 条目计数</span>
    <span class="n">articles</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># csv内容存入</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">w_num</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>  <span class="c1"># 按页爬取</span>
        <span class="n">url</span> <span class="o">=</span> <span class="n">base_url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">page</span><span class="o">=</span><span class="n">i</span><span class="p">)</span>  <span class="c1"># 按照网页链接规律</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>  <span class="c1"># 打开网页</span>
            <span class="c1"># lxml解析器 速度快 文档纠错能力强 需要安装C语言库</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>  <span class="c1"># 缓冲</span>
            <span class="n">titles</span> <span class="o">=</span> <span class="n">BeautifulSoup</span><span class="p">(</span><span class="n">driver</span><span class="o">.</span><span class="n">page_source</span><span class="p">,</span> <span class="s1">&#39;lxml&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">find_all</span><span class="p">(</span><span class="s1">&#39;div&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="s1">&#39;text&#39;</span><span class="p">})</span>  <span class="c1"># 解析div部分代码</span>
            <span class="k">if</span> <span class="n">titles</span><span class="p">:</span>
                <span class="k">print</span><span class="p">(</span><span class="s2">&#34;||||||------------第{0}页爬取内容已获得------------||||||&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">page</span><span class="p">))</span>
                <span class="k">for</span> <span class="n">title</span> <span class="ow">in</span> <span class="n">titles</span><span class="p">:</span>  <span class="c1"># 注意，xpath失败由于类型错误</span>
                    <span class="nb">id</span> <span class="o">=</span> <span class="n">num</span>
                    <span class="n">new_title</span> <span class="o">=</span> <span class="n">title</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">get_text</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>  <span class="c1"># 获取a标签内容并去除空格，回车</span>
                    <span class="n">new_summ</span> <span class="o">=</span> <span class="n">title</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;p&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="s1">&#39;info&#39;</span><span class="p">})</span><span class="o">.</span><span class="n">get_text</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
                    <span class="n">new_time</span> <span class="o">=</span> <span class="n">title</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;p&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="s1">&#39;time&#39;</span><span class="p">})</span><span class="o">.</span><span class="n">get_text</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
                    <span class="n">url</span> <span class="o">=</span> <span class="n">title</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">)[</span><span class="s1">&#39;href&#39;</span><span class="p">]</span>  <span class="c1"># 获取a标签href内容</span>
                    <span class="c1"># sub_ord(title, url)  # 提速开进程4</span>
                    <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                        <span class="n">make_path</span><span class="p">()</span>  <span class="c1"># 判断储存文件夹是否存在</span>
                    <span class="n">Identification</span><span class="p">(</span><span class="n">new_title</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span>  <span class="c1"># 爬取每页文章内容</span>
                    <span class="n">articles</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="nb">id</span><span class="p">,</span> <span class="n">new_title</span><span class="p">,</span> <span class="n">new_summ</span><span class="p">,</span> <span class="n">new_time</span><span class="p">,</span> <span class="n">url</span><span class="p">])</span>  <span class="c1"># 添加内容</span>
                    <span class="n">num</span> <span class="o">=</span> <span class="n">num</span> <span class="o">+</span> <span class="mi">1</span>
                    <span class="n">entry</span> <span class="o">=</span> <span class="n">entry</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="n">entry</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="k">print</span><span class="p">(</span><span class="s2">&#34;||||||------------第{0}页爬取完毕！！！------------||||||&#34;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">page</span><span class="p">))</span>
                <span class="n">page</span> <span class="o">=</span> <span class="n">page</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">print</span><span class="p">(</span><span class="s2">&#34;请检查网页结构是否发生变化&#34;</span><span class="p">)</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
                <span class="nb">exit</span><span class="p">()</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="s2">&#34;请更换ip或增加代理池，预防黑名单&#34;</span><span class="p">)</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
            <span class="nb">exit</span><span class="p">()</span>
    <span class="n">save_data</span><span class="p">(</span><span class="n">articles</span><span class="p">)</span>  <span class="c1"># 存储文件</span>

<span class="c1"># 识别模式</span>
<span class="k">def</span> <span class="nf">Identification</span><span class="p">(</span><span class="n">title</span><span class="p">,</span> <span class="n">url</span><span class="p">):</span>
    <span class="n">g</span> <span class="o">=</span> <span class="n">Goose</span><span class="p">({</span><span class="s1">&#39;stopwords_class&#39;</span><span class="p">:</span> <span class="n">StopWordsChinese</span><span class="p">})</span>  <span class="c1"># 使用中文包</span>
    <span class="n">article</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">extract</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">url</span><span class="p">)</span>  <span class="c1"># 识别url</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="s2">&#34;.</span><span class="se">\\</span><span class="s2">Content</span><span class="se">\\</span><span class="s2">&#34;</span> <span class="o">+</span> <span class="n">title</span> <span class="o">+</span> <span class="s2">&#34;.txt&#34;</span>  <span class="c1"># 存储路径与名称</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">article</span><span class="o">.</span><span class="n">cleaned_text</span><span class="p">[</span><span class="mi">0</span><span class="p">:])</span>  <span class="c1"># 写入内容</span>
    <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="s2">&#34;文件发生异常，请检查文件！！！&#34;</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
        <span class="nb">exit</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">make_path</span><span class="p">():</span>
    <span class="k">if</span> <span class="s2">&#34;Content&#34;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">():</span>
        <span class="k">if</span> <span class="nb">input</span><span class="p">(</span><span class="s2">&#34;当前目录下存储文件夹Content已存在，是否需要删除？Y/N:   &#34;</span><span class="p">)</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;Y&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">]:</span>
            <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="s2">&#34;Content&#34;</span><span class="p">)</span>
            <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="s2">&#34;Content&#34;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="s2">&#34;Content&#34;</span><span class="p">)</span>

<span class="c1"># 普通模式, 速度太慢！！！</span>
<span class="c1"># def sub_ord(sub_title, url):</span>
<span class="c1">#     driver.get(url)</span>
<span class="c1">#     titles = BeautifulSoup(driver.page_source, &#39;lxml&#39;).find_all(&#39;div&#39;, {&#39;class&#39;: &#39;Body&#39;})</span>
<span class="c1">#     if titles:</span>
<span class="c1">#         for title in titles:</span>
<span class="c1">#             new_summ = title.get_text().replace(&#39; &#39;, &#39;&#39;).strip(&#39;\n&#39;)</span>
<span class="c1">#             print(new_summ)</span>
<span class="c1">#     else:</span>
<span class="c1">#         print(&#34;请检查网页结构是否发生变化&#34;)</span>
<span class="c1">#         time.sleep(5)</span>
<span class="c1">#         exit()</span>
<span class="c1">#     try:</span>
<span class="c1">#         filename = sub_title + &#34;.txt&#34;</span>
<span class="c1">#         with open(filename, &#39;w&#39;) as f:</span>
<span class="c1">#             f.write(new_summ)</span>
<span class="c1">#     except IOError:</span>
<span class="c1">#         print(&#34;文件发生异常，请检查文件！！！&#34;)</span>
<span class="c1">#         f.close()</span>
<span class="c1">#         time.sleep(5)</span>
<span class="c1">#         exit()</span>
<span class="c1">#     driver.close()</span>

<span class="c1"># def Content(driver):</span>
<span class="c1">#         try:</span>
<span class="c1">#             links = driver.find_elements_by_xpath(&#34;//*[@id=&#39;newsListContent&#39;]/li[&#34; + str(i) + &#34;]/div[2]/p[1]/a&#34;)</span>
<span class="c1">#             for link in links:</span>
<span class="c1">#                 urls.append(link.get_attribute(&#39;href&#39;))</span>
<span class="c1">#         except:</span>
<span class="c1">#             print(&#34;写法出现错误&#34;)</span>

<span class="k">def</span> <span class="nf">save_data</span><span class="p">(</span><span class="n">articles</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;save.csv&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">writer</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">writer</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
            <span class="n">writer</span><span class="o">.</span><span class="n">writerow</span><span class="p">([</span><span class="s1">&#39;文章id&#39;</span><span class="p">,</span> <span class="s1">&#39;文章标题&#39;</span><span class="p">,</span> <span class="s1">&#39;文章摘要&#39;</span><span class="p">,</span> <span class="s1">&#39;发表时间&#39;</span><span class="p">,</span> <span class="s1">&#39;文章链接&#39;</span><span class="p">])</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">articles</span><span class="p">:</span>
                    <span class="n">writer</span><span class="o">.</span><span class="n">writerow</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
                <span class="k">print</span><span class="p">(</span><span class="s2">&#34;保存完毕！！！随时停止&#34;</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="k">print</span><span class="p">(</span><span class="s2">&#34;文件写入发生异常，请检查数据！！！&#34;</span><span class="p">)</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
                <span class="nb">exit</span><span class="p">()</span>
    <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="s2">&#34;文件发生异常，请检查文件！！！&#34;</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
        <span class="nb">exit</span><span class="p">()</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="c1"># w_num = int(input(&#34;请输入爬取页面数量: &#34;))</span>
    <span class="n">w_num</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="n">driver</span> <span class="o">=</span> <span class="n">nChrome</span><span class="p">()</span>
    <span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">EC</span><span class="o">.</span><span class="n">alert_is_present</span><span class="p">()(</span><span class="n">driver</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">result</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="s2">&#34;alert 存在弹窗，处理后再试验&#34;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="s2">&#34;alert 未弹出！&#34;</span><span class="p">)</span>
            <span class="k">break</span>
    <span class="n">Open_w</span><span class="p">(</span><span class="n">w_num</span><span class="p">)</span>
    <span class="c1"># content = driver.page_source.encode(&#39;utf-8&#39;)</span>
    <span class="n">confirm</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">&#34;数据已成功爬取并存储，是否查看？Y（查看文件）|N（关闭进程）：&#34;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">confirm</span> <span class="o">==</span> <span class="s1">&#39;Y&#39;</span> <span class="ow">or</span> <span class="n">confirm</span> <span class="o">==</span> <span class="s1">&#39;y&#39;</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">os</span>
        <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s2">&#34;start save.csv&#34;</span><span class="p">)</span>
        <span class="n">driver</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">driver</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</code></pre></div>
                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/%E7%88%AC%E8%99%AB/">爬虫</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/selenium/">selenium</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/beautifulsoup/">BeautifulSoup</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/goose3/">goose3</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-04/linux%E4%B8%8Bwhichwhereislocatefind%E7%9A%84%E5%8C%BA%E5%88%AB/">
                <h2 class="post-title">Linux下which、whereis、locate、find的区别</h2>

                
              </a>

              <p class="post-meta">
                Posted on April 20, 2019
              </p>
              <div class="post-entry">
                
                  <h1 id="linux下-which--whereis--locate--find的区别">Linux下 which | whereis | locate | find的区别</h1>
<ul>
<li><code>which</code>      查看可执行文件的位置</li>
<li><code>whereis</code>    查看文件的位置</li>
<li><code>locate</code>     配合数据库查看文件位置</li>
<li><code>find</code>       实际搜寻硬盘查询文件名称</li>
</ul>
<h2 id="1-find">1. find</h2>
<p>find是最常用和最强大的查找命令。它能做到实时查找，精确查找，但速度慢。</p>
<p>find的使用格式如下：</p>
<blockquote>
<p>find [指定目录] [指定条件] [指定动作]</p>
</blockquote>
<ul>
<li>指定目录：是指所要搜索的目录和其子目录。如果不指定，则默认为当前目录。</li>
<li>指定条件：是指所要搜索的文件的特点。</li>
<li>指定动作：是指对搜索的结果如何处理。</li>
</ul>
<p><strong>常见选项:</strong></p>
<ul>
<li>-name  根据文件名称查找（严格区分大小写，如果文件名中间有空格，则必须把文件名用双引号引起来）</li>
<li>-iname  根据文件名查找（不区分大小写）</li>
<li>-regex &ldquo;PATTERN&rdquo; 正则表达式匹配</li>
<li>-user  根据文件属主进行查找</li>
<li>-group  根据文件属组进行查找</li>
<li>-uid  根据文件的uid查找</li>
<li>-gid   根据用户的gid查找</li>
<li>-nouser  查看所有的没有属主的文件</li>
<li>-nogroup   查看所有的没有属组的文件</li>
</ul>
<p><strong>举例</strong></p>
<ul>
<li>查看tmp目录下，文件属主为redhat的文件</li>
</ul>
<pre><code>#ls -l `find /tmp -user redhat`
</code></pre><ul>
<li>查看根目录下文件或文件名为xxx的文件或文件夹</li>
</ul>
<pre><code>#find / -name xxx
</code></pre><h2 id="2-whereis">2. whereis</h2>
<p>whereis命令只能用于搜索程序名，而且只搜索二进制文件（参数-b）、man说明文件（参数-m）和源代码文件（参数-s）。如果省略参数，则返回所有信息。</p>
<p>语法：</p>
<blockquote>
<p>whereis [-bmsu] 文件或者目录名称</p>
</blockquote>
<p>参数说明：</p>
<ul>
<li><code>-b</code> ： 只找二进制文件</li>
<li><code>-m</code>： 只找在说明文件manual路径下的文件</li>
<li><code>-s</code> ： 只找source源文件</li>
<li><code>-u</code> ： 没有说明文档的文件
例如：</li>
</ul>
<pre><code>[root@redhat ~]# whereis passwd 
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz
将和passwd文件相关的文件都查找出来 

[root@redhat ~]# whereis -b passwd 
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd 
只将二进制文件 查找出来 
</code></pre><p><strong>和find相比，whereis查找的速度非常快</strong></p>
<p>这是因为linux系统会将系统内的所有文件都记录在一个数据库文件中，当使用whereis和下面即将介绍的locate时，会从数据库中查找数据，而不是像find命令那样，通过遍历硬盘来查找，效率自然会很高。</p>
<p>但是该数据库文件并不是实时更新，默认情况下时一星期更新一次，因此，我们在用whereis和locate 查找文件时，有时会找到已经被删除的数据，或者刚刚建立文件，却无法查找到，原因就是因为数据库文件没有被更新。</p>
<h2 id="3--which">3.  which</h2>
<p>which命令是查找命令是否存在，以及命令的存放位置在哪儿。</p>
<p>语法：</p>
<blockquote>
<p>which 可执行文件名称</p>
</blockquote>
<p>例如：</p>
<pre><code>[root@sys ~]# which passwd 
/usr/bin/passwd 
</code></pre><p>which是通过 PATH 环境变量到该路径内查找可执行文件，所以基本的功能是寻找可执行文件</p>
<h2 id="4-locate">4. locate</h2>
<p><strong>locate命令不是实时查找，所以查找的结果不精确，但查找速度很快。</strong></p>
<p>因为它查找的不是目录，而是一个数据库（/var/lib /locatedb），这个数据库中含有本地所有文件信息。</p>
<p>Linux系统自动创建这个数据库，并且每天自动更新一次，所以使用locate命令查不到最 新变动过的文件。</p>
<p>为了避免这种情况，可以在使用locate之前，先使用updatedb命令，手动更新数据库。</p>
<p>格式</p>
<blockquote>
<p>locate 文件或者目录名称</p>
</blockquote>
<p>例如：</p>
<pre><code>查找所有包含passwd的文件
#locate passwd

查找所有包含passwd的文件，并且只显示前十个
#locate -n 10 passwd
</code></pre>
                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/linux/">Linux</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E6%9F%A5%E6%89%BE/">查找</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-04/python%E5%88%97%E8%A1%A8%E8%A7%A3%E6%9E%90%E5%BC%8F/">
                <h2 class="post-title">Python列表解析式</h2>

                
              </a>

              <p class="post-meta">
                Posted on April 6, 2019
              </p>
              <div class="post-entry">
                
                  <p>照例先是官方文档：<a href="https://docs.python.org/zh-cn/3/tutorial/datastructures.html#tut-listcomps">python列表解析(推导式)</a></p>
<p>原文链接: <a href="http://codingpy.com/article/python-list-comprehensions-explained-visually/">轻松学会Python列表解析式</a></p>
<p>还有一个：<a href="http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/">原文</a></p>
<h2 id="什么是列表解析式">什么是列表解析式？</h2>
<p>列表解析式是将一个列表（实际上适用于任何<a href="https://docs.python.org/3/glossary.html#term-iterable">可迭代对象（iterable）</a>）转换成另一个列表的工具。在转换过程中，可以指定元素必须符合一定的条件，才能添加至新的列表中，这样每个元素都可以按需要进行转换。</p>
<p>如果你熟悉函数式编程（functional programming），你可以把列表解析式看作为结合了 <code>filter</code> 函数与 <code>map</code> 函数功能的语法糖：</p>
<blockquote>
<p>doubled_odds = map(lambda n: n * 2, filter(lambda n: n % 2 == 1, numbers))
doubled_odds = [n * 2 for n in numbers if n % 2 == 1]</p>
</blockquote>
<p>如果你不熟悉函数式编程，也不用担心：我稍后会通过<code>for</code>循环为大家讲解。</p>
<h2 id="从循环到解析式">从循环到解析式</h2>
<p>每个列表解析式都可以重写为for循环，但不是每个for循环都能重写为列表解析式。</p>
<p>掌握列表解析式使用时机的关键，在于不断练习识别那些看上去像列表解析式的问题（practice identifying problems that smell like list comprehensions）。</p>
<p>如果你能将自己的代码改写成类似下面这个for循环的形式，那么你也就可以将其改写为列表解析式：</p>
<pre><code>new_things = []
for ITEM in old_things:
    if condition_based_on(ITEM):
        new_things.append(&quot;something with &quot; + ITEM)
</code></pre><p>你可以将上面的for循环改写成这样的列表解析式：</p>
<pre><code>new_things = [&quot;something with &quot; + ITEM for ITEM in old_things if condition_based_on(ITEM)]
</code></pre><h2 id="列表解析式可视化解读">列表解析式：可视化解读</h2>
<p>可视化解读听上去是个不错的注意，但是我们怎么才能做到这点呢？</p>
<p>嘿嘿，只需要从for循环中复制粘贴，稍微调整一下就变成了列表解析式啦。
<img src="https://github.com/systemime/my_image/blob/master/python%E5%88%97%E8%A1%A8%E8%A7%A3%E6%9E%90%E5%BC%8F/D1.gif?raw=true" alt="动图1">
下面是我们复制粘贴的顺序：</p>
<ul>
<li>将变量赋值操作复制到新建的空列表中（第三行）</li>
<li>将<code>append()</code>方法中的表达式参数复制到新列表中（第六行）</li>
<li>复制<code>for</code>循环语句，不包括最后的:（第四行）</li>
<li>复制<code>if</code>条件控制语句，同样不包括最后的:（第五行）</li>
</ul>
<p>这样，我们将从下面这段代码：</p>
<pre><code>numbers = [1, 2, 3, 4, 5]

doubled_odds = []
for n in numbers:
    if n % 2 == 1:
        doubled_odds.append(n * 2)
</code></pre><p>转换成了这两行代码：</p>
<pre><code>numbers = [1, 2, 3, 4, 5]

doubled_odds = [n * 2 for n in numbers if n % 2 == 1]
</code></pre><h2 id="无条件子句的列表解析式">无条件子句的列表解析式</h2>
<p>如果是那些没有条件子句（即 <code>if SOMETHING</code> 部分）的代码呢，又该怎样复制粘贴？这些形式的代码甚至比有条件子句的代码更好实现。</p>
<p>一个没有<code>if</code>语句的<code>for</code>循环：</p>
<pre><code>doubled_numbers = []
for n in numbers:
    doubled_numbers.append(n * 2)
</code></pre><p>上面这段代码页可以改写为一个列表解析式：</p>
<pre><code>doubled_numbers = [n * 2 for n in numbers]
</code></pre><p>下面是转换过程的详细演示：
<img src="https://github.com/systemime/my_image/blob/master/python%E5%88%97%E8%A1%A8%E8%A7%A3%E6%9E%90%E5%BC%8F/D2.gif?raw=true" alt="动图2">
我们可以从上面那个简单的for循环中，安装这样的顺序复制粘贴：</p>
<ul>
<li>将变量赋值操作复制到新建的空列表中（第三行）</li>
<li>将<code>append()</code>方法中的表达式参数复制到新列表中（第五行）</li>
<li>复制for循环语句，不包括最后的:（第四行）</li>
</ul>
<h2 id="嵌套循环">嵌套循环</h2>
<p>那么嵌套循环（nested loop）又该怎样改写为列表解析式呢？</p>
<p>下面是一个拉平（flatten）矩阵（以列表为元素的列表）的for循环：</p>
<pre><code>flattened = []
for row in matrix:
    for n in row:
        flattened.append(n)
</code></pre><p>下面这个列表解析式实现了相同的功能：</p>
<pre><code>flattened = [n for row in matrix for n in row]
</code></pre><p>列表解析式中的嵌套循环读起来就有点绕口了。</p>
<p>注意：我本能地会想把这个列表解析式写成这样：</p>
<pre><code>flattened = [n for n in row for row in matrix]
</code></pre><p>但是这行代码是错误的。这里我不小心颠倒了两个<code>for</code>循环的顺序。正确的代码是之前那个。</p>
<p>如果要在列表解析式中处理嵌套循环，请记住** <code>for</code>循环子句的顺序与我们原来<code>for</code>循环的顺序是一致的**。</p>
<p>同样地原则也适用集合解析式（set comprehension）和字典解析式（dictionary comprehension）。</p>
<h2 id="其他解析式">其他解析式</h2>
<p>下面的代码提取单词序列中每个单词的首字母，创建了一个集合（set）：</p>
<pre><code>first_letters = set()
for w in words:
    first_letters.add(w[0])
</code></pre><p>同样的代码可以改写为集合解析式：</p>
<pre><code>first_letters = {w[0] for w in words}
</code></pre><p>下面的代码将原有字典的键和值互换，从而创建了一个新的字典：</p>
<pre><code>flipped = {}
for key, value in original.items():
    flipped[value] = key
</code></pre><p>同样的代码可以改写为字典解析式：</p>
<pre><code>flipped = {value: key for key, value in original.items()}
</code></pre><h2 id="还要注意可读性">还要注意可读性</h2>
<p>你有没有发现上面的列表解析式读起来很困难？我经常发现，如果较长的列表解析式写成一行代码，那么阅读起来就非常困难。</p>
<p>不过，还好Python支持在括号和花括号之间断行。</p>
<p><strong>列表解析式 List comprehension</strong>
断行前：</p>
<pre><code>doubled_odds = [n * 2 for n in numbers if n % 2 == 1]
</code></pre><p>断行后：</p>
<pre><code>doubled_odds = [
    n * 2
    for n in numbers
    if n % 2 == 1
]
</code></pre><p>** 带嵌套循环的列表解析式 **
断行前：</p>
<pre><code>flattened = [n for n in row for row in matrix]
</code></pre><p>断行后：</p>
<pre><code>flattened = [
    n
    for row in matrix
    for n in row
]
</code></pre><p>** 字典解析式 **
断行前：</p>
<pre><code>flipped = {value: key for key, value in original.items()}
</code></pre><p>断行后：</p>
<pre><code>flipped = {
    value: key
    for key, value in original.items()
}
</code></pre><p>请注意，我们并不是随意进行断行：我们是在每一行复制过来的代码之后断行的。</p>
<h2 id="总结">总结</h2>
<p>纠结于写不出列表解析式吗？不要担心。先写一个for循环，能后按照本文说的顺序复制粘贴，就可以写出解析式了。</p>
<p>任何类似下面代码形式的for循环：</p>
<pre><code>new_things = []
for ITEM in old_things:
    if condition_based_on(ITEM):
        new_things.append(&quot;something with &quot; + ITEM)
</code></pre><p>都可以被改写为下面这种列表解析式：</p>
<pre><code>new_things = [&quot;something with &quot; + ITEM for ITEM in old_things if condition_based_on(ITEM)]
</code></pre>
                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/python/">Python</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E5%88%97%E8%A1%A8%E8%A7%A3%E6%9E%90/">列表解析</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-04/django%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%BD%91%E9%A1%B5%E5%BC%B9%E5%87%BA%E8%AD%A6%E5%91%8A/">
                <h2 class="post-title">Django实现简单网页弹出警告</h2>

                
              </a>

              <p class="post-meta">
                Posted on April 6, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="功能所在位置">功能所在位置</h2>
<p>该功能运用了django.contrib.messages这个库，再django项目中srtting.py文件中的38行，APP注册部分</p>
<pre><code>INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01.apps.App01Config',  # 自定义APP注册
]
</code></pre><h2 id="导入库">导入库</h2>
<pre><code>from django.contrib import messages

def abc(request):
    messages.success(request, &quot;哈哈哈&quot;)
</code></pre><h2 id="前台代码添加">前台代码添加</h2>
<p>需在html页面上显示这个消息提示框，代码如下，但必须注意的是如果页面有多个表单的时候，只需要在表单结束后加一次就够了，view底下不同的函数可以显示不同的消息：</p>
<pre><code>{% if messages %}
    &lt;script&gt;
        {% for msg in messages %}
            alert('{{ msg.message }}');
        {% endfor %}
    &lt;/script&gt;
{% endif %}
</code></pre>
                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/django/">Django</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/messages/">messages</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E5%BC%B9%E7%AA%97/">弹窗</a>&nbsp;
                  
                </span>
              

            </article>
          
        </div>
        
          <ul class="pager main-pager">
            
              <li class="previous">
                <a href="https://systemime.github.io/blog/page/8">&larr; Newer Posts</a>
              </li>
            
            
              <li class="next">
                <a href="https://systemime.github.io/blog/page/10">Older Posts &rarr;</a>
              </li>
            
          </ul>
        
      </div>
    <div align="center" class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        

<ul class="pagination">
    
    <li class="page-item">
        <a href="https://systemime.github.io/blog/" class="page-link" aria-label="First"><span aria-hidden="true">&laquo;&laquo;</span></a>
    </li>
    
    <li class="page-item">
    <a href="https://systemime.github.io/blog/page/8/" class="page-link" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a>
    </li>
    
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/blog/">1</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/blog/page/2/">2</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/blog/page/3/">3</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item disabled"><span aria-hidden="true">&nbsp;&hellip;&nbsp;</span></li>
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/blog/page/8/">8</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item active"><a class="page-link" href="https://systemime.github.io/blog/page/9/">9</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/blog/page/10/">10</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item disabled"><span aria-hidden="true">&nbsp;&hellip;&nbsp;</span></li>
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/blog/page/18/">18</a></li>
    
    
    <li class="page-item">
    <a href="https://systemime.github.io/blog/page/10/" class="page-link" aria-label="Next"><span aria-hidden="true">&raquo;</span></a>
    </li>
    
    <li class="page-item">
        <a href="https://systemime.github.io/blog/page/18/" class="page-link" aria-label="Last"><span aria-hidden="true">&raquo;&raquo;</span></a>
    </li>
    
</ul>


        
        
        
        
        
      </div>
    </div>
  </div>

    <div class="rocket"><img src="https://systemime.github.io/img/rocket_up.png" alt="" width="100" height="100"></div>
<script>
    $(function () {
      $(window).scroll(function () {
        
        if ($(window).scrollTop() >= 1000) {
          $('.rocket').stop().fadeIn(1000);
        }else {
          $('.rocket').stop().fadeOut(1000);
        }
      })
      
      $('.rocket').click(function () {
        $('html,body').stop().animate({scrollTop:0},400);
       
      })
    })

</script>

    
    <div id="landlord">
        <div class="message" style="opacity:0"></div>
        <canvas id="live2d" width="280" height="250" class="live2d"></canvas>
        <div class="hide-button">隐藏</div>
    </div>
    
    <footer id="copyright">
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <ul class="list-inline text-center footer-links">
          
          
        </ul>
        <p class="credits copyright text-muted">
        &copy;2017-2018
          
          &nbsp;&bull;&nbsp;
          September 20,2019
          updated
          
            &nbsp;&bull;&nbsp;
            <a href="https://systemime.github.io">Home</a>
          
        </p>

        <p class="credits theme-by text-muted">
        <a href="https://servicemesher.github.io" target="_blank">ServiceMesher</a>&nbsp;&bull;&nbsp;<a href="https://github.com/yangchuansheng/k8s-knowledge" target="_blank">Kubernetes 知识图谱</a>
        <br />
        <span id="busuanzi_container_site_pv">
            本站已被访问 <span id="busuanzi_value_site_pv"></span> 次啦
        </span>
        <span id="busuanzi_container_site_uv">
            &ensp;|&ensp;&thinsp;您是第 <span id="busuanzi_value_site_uv"></span> 位到访的小伙伴喔
        </span>
        </p>
        
        <p class="credits theme-by text-muted">
          <a href="http://gohugo.io">Hugo v0.67.0</a> powered &nbsp;&bull;&nbsp; Theme by <a href="http://deanattali.com/beautiful-jekyll/">Beautiful Jekyll</a> adapted to <a href="https://github.com/halogenica/beautifulhugo">Beautiful Hugo</a>
          
          <center><section class="credits theme-by text-muted">
    <span class="footer__copyright">
    <div>
    <span id="span_dt_dt"> </span>
    <script language="javascript">
      function show_date_time(){
        window.setTimeout("show_date_time()", 1000);
        BirthDay=new Date("7/8/2017 10:56:12");
        today=new Date();
        timeold=(today.getTime()-BirthDay.getTime());
        sectimeold=timeold/1000
        secondsold=Math.floor(sectimeold);
        msPerDay=24*60*60*1000
        e_daysold=timeold/msPerDay
        daysold=Math.floor(e_daysold);
        e_hrsold=(e_daysold-daysold)*24;
        hrsold=Math.floor(e_hrsold);
        e_minsold=(e_hrsold-hrsold)*60;
        minsold=Math.floor((e_hrsold-hrsold)*60);
        seconds=Math.floor((e_minsold-minsold)*60);
        span_dt_dt.innerHTML="本博客已经开心运行 "+daysold+" 天 "+hrsold+" 小时 "+minsold+" 分 "+seconds+" 秒";
      }
      show_date_time();
    </script>
    </div>
</script>
</section>
</center>
        </p>
      </div>
    </div>
  </div>
</footer>


<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/bootstrap.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/photoswipe-ui-default.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/photoswipe.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/auto-render.min.js"></script>
<script src="https://systemime.github.io/js/main.js"></script>
<script src="https://systemime.github.io/js/clipboard.min.js"></script>
<script src="https://systemime.github.io/js/prism.js?t=123"></script>
<script src="https://systemime.github.io/js/highlight.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/katex.min.js"></script>
<script src="https://systemime.github.io/js/reward.js"></script>
<script src="https://systemime.github.io/js/canvas_ribbon.js"></script>
<script> renderMathInElement(document.body); </script>

<script src="https://systemime.github.io/js/baguetteBox.js"></script>
<script> baguetteBox.run('.gallery');</script>






<script async defer src="https://buttons.github.io/buttons.js"></script>


<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
<script type="text/javascript" src="https://systemime.github.io/js/quicklink.umd.js"></script>
<script>
window.addEventListener('load', () =>{
   quicklink();
});
</script>



<script type="text/javascript">
 
var a_idx = 0;
jQuery(document).ready(function($) {
    $("body").click(function(e) {
        var a = new Array("Docker", "Kubernetes", "Prometheus", "Envoy", "Istio", "Service Mesh", "Cloud Native");
        var $i = $("<span />").text(a[a_idx]);
        a_idx = (a_idx + 1) % a.length;
        var x = e.pageX,
        y = e.pageY;
        function randomColor() {
          var flakeColor = new Array("#FFDA65", "#00BFFF", "#BA55D3", "#FFA07A", "#87CEEB", "#FFB6C1");
          var snowColor = flakeColor[Math.floor(flakeColor.length * Math.random())];
          return snowColor;
        }
        $i.css({
            "z-index": 999999999999999999999999999999999999999999999999999999999999999999999,
            "top": y - 20,
            "left": x,
            "position": "absolute",
            "font-weight": "bold",
            "color": randomColor()
        });
        $("body").append($i);
        $i.animate({
            "top": y - 180,
            "opacity": 0
        },
        1500,
        function() {
            $i.remove();
        });
    });
});
</script>


<script type="text/javascript" src="https://systemime.github.io/js/lightbox.js"></script>











<script src="https://cdn.plyr.io/3.4.7/plyr.js"></script>
<script>const player = new Plyr('#player');</script>


<script async defer src="https://buttons.github.io/buttons.js"></script>


  </body>
</html>

